package xcmg.device.service.sale;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.sale.SalesBillDetailDO;
import xcmg.device.dao.mapper.sale.SalesBillDetailMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.vo.sale.SalesBillDetailVO;
import yb.ecp.fast.feign.FastGenClient;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wrg on 2019/4/22.
 */
@Service
public class SalesBillDetailService {

    @Autowired
    private SalesBillDetailMapper salesBillDetailMapper;
    @Autowired
    private FastGenClient genClient;

    public List<SalesBillDetailVO> list(SalesBillDetailVO condition) {
        List<SalesBillDetailDO> doList = salesBillDetailMapper.list(condition);
        List<SalesBillDetailVO> voList = new ArrayList<>();
        for (SalesBillDetailDO entity : doList) {
            SalesBillDetailVO model = new SalesBillDetailVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        return voList;
    }

    public ErrorCode add(List<SalesBillDetailVO> cond) {
        for (SalesBillDetailVO salesBillDetailVO : cond) {
            String id = genClient.newGuidText().getValue();
            if (StringUtils.isBlank(id)) {
                return ErrorCode.Failure;
            }
            SalesBillDetailDO detailDO = new SalesBillDetailDO();
            BeanUtils.copyProperties(salesBillDetailVO, detailDO);
            detailDO.setId(id);

            int insertCount = salesBillDetailMapper.insertSelective(detailDO);
            if (insertCount <= 0) {
                return ErrorCode.Failure;
            }
        }
        return ErrorCode.Success;
    }

    public ErrorCode delete(String salasBillNo) {
        return salesBillDetailMapper.deleteBySalasBillNo(salasBillNo) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public ErrorCode update(List<SalesBillDetailVO> cond, String salasBillNo) {
        ErrorCode errorCode = this.delete(salasBillNo);
        if (ErrorCode.Success != errorCode) {
            return ErrorCode.Failure;
        }

        ErrorCode add = this.add(cond);
        if (ErrorCode.Success != add) {
            return ErrorCode.Failure;
        }
        return ErrorCode.Success;
    }


    public boolean updateByOutBound(String salesBillNo, String materialsNo,Integer status){
        return salesBillDetailMapper.updateStatusByMelNoSalesNo(salesBillNo,materialsNo,status)>0;
    }

    public Integer checkWhetherNoOutbound(String salesBillNo){
        return salesBillDetailMapper.checkWhetherNoOutbound(salesBillNo);
    }

    public SalesBillDetailDO getSalesBillDetailDO(String salesBillNo, String materialsNo){
        return salesBillDetailMapper.getInfoByBillNoMate(salesBillNo,materialsNo);
    }

    public boolean updateCreateOutQty(String salesBillNo, String materialsNo,BigDecimal createOutQty,BigDecimal actualQty,BigDecimal oldThisShipQty){
        SalesBillDetailDO salesBillDetailDO = getSalesBillDetailDO(salesBillNo, materialsNo);
        if (salesBillDetailDO == null) {
            return false;
        }

        SalesBillDetailDO detailDO = new SalesBillDetailDO();
        if (null != createOutQty ){
            BigDecimal alreadyCreateQty = salesBillDetailDO.getCreateOutboundQty() == null ? BigDecimal.ZERO : salesBillDetailDO.getCreateOutboundQty();
            BigDecimal createOutboundQty;
            if (oldThisShipQty != null && oldThisShipQty.compareTo(BigDecimal.ZERO) != 0){
                createOutboundQty = alreadyCreateQty.subtract(oldThisShipQty).add(createOutQty);
            }else{
                createOutboundQty = alreadyCreateQty.add(createOutQty);
            }

            if (salesBillDetailDO.getApplyQty().compareTo(createOutboundQty) == 0){
                detailDO.setCreateOutboundStatus(2);
            }else {
                detailDO.setCreateOutboundStatus(1);
            }
            detailDO.setCreateOutboundQty(createOutboundQty);
        }

        if (null != actualQty ){
            BigDecimal alreadyActualQty = salesBillDetailDO.getActualOutboundQty() == null ? BigDecimal.ZERO : salesBillDetailDO.getActualOutboundQty();
            if (salesBillDetailDO.getApplyQty().compareTo(alreadyActualQty.add(actualQty)) == 0){
                detailDO.setOutboundStatus(2);
            }else {
                detailDO.setOutboundStatus(1);
            }
            detailDO.setActualOutboundQty(alreadyActualQty.add(actualQty));
        }
        detailDO.setSalesBillNo(salesBillNo);
        detailDO.setMaterialNo(materialsNo);
        return salesBillDetailMapper.updateCreateOutQty(detailDO)>0;
    }


    public Integer checkOutboundStatus(String salesBillNo){
        return salesBillDetailMapper.checkOutboundStatus(salesBillNo);
    }

    public Integer checkCreateOutboundStatus(String salesBillNo){
        return salesBillDetailMapper.checkCreateOutboundStatus(salesBillNo);
    }

    public boolean updateOutQty(String salesBillNo, String materialsNo,BigDecimal createOutQty){
        SalesBillDetailDO salesBillDetailDO = getSalesBillDetailDO(salesBillNo, materialsNo);
        if (salesBillDetailDO == null) {
            return false;
        }
        BigDecimal createOutboundQty = salesBillDetailDO.getCreateOutboundQty();

        SalesBillDetailDO detailDO = new SalesBillDetailDO();
        detailDO.setCreateOutboundQty(createOutboundQty.subtract(createOutQty));
        detailDO.setSalesBillNo(salesBillNo);
        detailDO.setMaterialNo(materialsNo);
        return salesBillDetailMapper.updateCreateOutQty(detailDO)>0;
    }
}
